package org.plants.algo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

import org.plants.additional_info.Pair;

public class PufferTrain {
	public static int MIN_TURNS = 150;
	public static double TOLERANCE = 80.0;
	public static int PLANTS_NUM = 22;
	public static Pair SPACE_DIM = new Pair(7, 167);
	public static int PRIORITY = 6;
	
	private static int getFiguresAtLine(int poolSize) {
		if ((poolSize / SPACE_DIM.getFirst()) > 0)
			return 1;
		return 0;
	}
	private static int getFiguresAtRaw(int poolSize) {
		return poolSize / SPACE_DIM.getSecond();
	}
	
	private static void initializePlantMap(Map<Integer, Vector<Integer>> map) {
		for (Integer i = 0; i < SPACE_DIM.getSecond(); i++) {
			Vector<Integer> temp = new Vector<Integer>();
			switch (i) {
			case 75: case 89:
				temp.add(4);
				map.put(i, temp);
				break;
			case 76: case 90:
				temp.add(5);
				map.put(i, temp);
				break;
			case 77: case 91:
				temp.add(1); temp.add(5);
				map.put(i, temp);
				break;
			case 78: case 92:
				temp.add(2); temp.add(3); temp.add(4); temp.add(5);
				map.put(i, temp);
				break;
			case 82:
				temp.add(1);
				map.put(i, temp);
				break;
			case 83:
				temp.add(2); temp.add(3);
				map.put(i, temp);
				break;
			case 84: case 85:
				temp.add(3);
				map.put(i, temp);
				break;
			case 86:
				temp.add(2);
				map.put(i, temp);
				break;
			}
		}
	}
	
	public static String getName() {
		return "PufferTrain";
	}
	
	public static boolean isSuitableFigure(int inputPlantsNumber, int poolSize, int lifeDuration) {
		if ((lifeDuration >= MIN_TURNS)
				&& (poolSize >= SPACE_DIM.getSecond())
				&& (inputPlantsNumber >= PLANTS_NUM)
				&& (100 >= inputPlantsNumber)) {
				return true;
		}
		return false;
	}
	
	public static void fillPool(Character[][] pool, int inputPlantsNumber, int poolSize) {
		int availablePlants = inputPlantsNumber;
		int columnNumber = 0;
		Map<Integer, Vector<Integer>> plantMap = new HashMap<Integer, Vector<Integer>>();
		initializePlantMap(plantMap);
		
		while (availablePlants > 0 && columnNumber < getFiguresAtRaw(poolSize)) {
			for (int i = 0; ((i < getFiguresAtLine(poolSize)) && (availablePlants > 0)); i++) {
				for (int j = 0; ((j < SPACE_DIM.getSecond()) && (availablePlants > 0)); j++) {
					if(!plantMap.get(j).isEmpty()) {
						Iterator<Integer> itr = plantMap.get(j).iterator();
						while((itr.hasNext()) && (availablePlants > 0)) {
							pool[j + (columnNumber * SPACE_DIM.getSecond())][(itr.next()) + (i * SPACE_DIM.getFirst())] = '+';
							availablePlants -= 1;
						}
					}
				}
			}
			columnNumber += 1;
		}
		
		//Fill the remaining
		int remainingPlants = availablePlants + (inputPlantsNumber % PLANTS_NUM);
		while(remainingPlants > 0) {
			Random ranGenerator = new Random();
			int i = 0;
			int j = 0;
			while(i < SPACE_DIM.getFirst()) {
				j = ranGenerator.nextInt(poolSize);
			}
			while(j < SPACE_DIM.getSecond()) {
				j = ranGenerator.nextInt(poolSize);
			}
			if (pool[i][j] != '+') {
				pool[i][j] = '+';
				remainingPlants -= 1;
			}
		}
	}
}
